package gameSite;

import java.util.ArrayList;
import javax.ejb.Stateful;

@Stateful
public class Cart {

	private static final long serialVersionUID = 1L;

	int sessionID;
	public static ArrayList<Product> productList = new ArrayList<Product>();
	double total=0.0d;
	public int productCount= 0;
	int userID;
	

	Cart(int sessionid, int userid, Product prod)
	{
		sessionID = sessionid;
		userID = userid;
		this.addProductToCart(prod);
	}
	
	Cart(int sessionid, int userid)
	{
		sessionID = sessionid;
		userID = userid;
	}
	
	boolean addProductToCart(Product x)
	{
		if(productListContains(x))
		{
			productList.add(x);
			productCount++;
			updateTotal();
			return true;
		}
		return false;
	}
	
	boolean removeProductFromCart(Product x)
	{
		if(productListContains(x))
		{
			return deleteProductFromProductList(x);
		}
		return false;
	}
	
	boolean productListContains(Product x)
	{
		for(Product y : productList)
		{
			if(y.getProductID() == x.getProductID())
			{
				return true;
			}
		}
		return false;
	}
	
	boolean deleteProductFromProductList(Product x)
	{
		int counter = 0;
		for(Product y : productList)
		{
			if(y.getProductID() == x.getProductID())
			{
				productList.remove(counter);
				productCount--;
				return true;
			}
			counter++;
		}
		return false;
	}
	
	void updateTotal()
	{
		total = total + productList.get(productList.size()-1).getPrice();
	}
	
	void emptyCart()
	{
		productList.clear();
	}
	
	int getSessionID(){return sessionID;}
	public ArrayList<Product> getProductList(){return productList;}
	double getTotal() {return total;}
	int getUserID(){return userID;}
	public int getProductCount(){return productCount;}
	
	void setSeesionID(int x ){sessionID= x;}
	void setProductList(ArrayList<Product> x){productList = x;}
	void setTotal (double x){total = x;}
	void setUserID(int x){userID =x;}
	
	
}
